<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/7
 * Time: 15:08
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Add extends Controller
{
    public function Add()
    {
        $user = Session::get('user');

        if (empty($user)){
            $msg = "您还没有登录，请先登录";
            $url = "/Log/login";
            $this->error($msg,$url);
        }


        // 查询问题分类表
        $cate = Db::table('zz_cate')->select();

        return view('index/add',[
            'cate' => $cate
        ]);
    }

    public function doAdd()
    {
        // 通过 session 下面得用户名 查询 当前用户得id
        $user = Session::get('user');
        $userId = Db::table('zz_user')->where('zz_name',$user['name'])->find();

        //        判断悬赏积分是否大于用户积分
        $jf = (int)input('reward');
        if (!empty($jf)){
            if ($jf > $userId['zz_integral']){
                return  $this->success('当前用户积分不足,先去评论赚点积分吧...');
            }
        }


        $p = input('post.');

        $data = array();
        $data['zz_uid'] = $userId['zz_id'];
        $data['zz_problem_tit'] = $p['title'];
        $data['zz_problem_cid'] = $p['cate'];
        $data['zz_problem_content'] = $p['content'];
        $data['zz_problem_jf'] = $p['reward'];
        $data['zz_problem_time'] = date('Y-m-d');

        $res = Db::table('zz_problem')->insert($data);

        if ($res){
            $msg = "问题提问成功，正在跳转。。";
            $url = "/";
            return $this->success($msg,$url);
        } else {
            return $this->error("提问失败");
        }
    }
}